﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeGraphicalControl;
using FeatureModelCore;
using Collections.TreeCollections;
using System.Windows.Controls;
using FeatureModelTool.FeatureModelView.MenuItems;
using FeatureModelTool.Windows;
using System.Windows;
using Collections;
using System.Windows.Media;
using FeatureModelCore.FMAnalyzer;

namespace FeatureModelTool.FeatureModelView.TreeViewItems
{
    public class TreeItem_Root : TreeViewItemControl
    {
        #region Properties
        public FeatureTreeRoot FeatureInstance { get; protected set; }
        protected Brush Backgroud
        {
            get
            {
                if (FeatureInstance.ExistError)
                    return FeatureTreeView.ERROR_BACKGROUND;
                if (FeatureInstance.ExistWarning)
                    return FeatureTreeView.WARNING_BACKGROUND;
                return FeatureTreeView.COMMON_BACKGROUND;
            }
        }
        #endregion

        private readonly static string _ICON_SOURCE = "/FeatureModelTool;component/Images/r.png";

        public TreeItem_Root(FeatureTreeRoot root)
        {
            FeatureInstance = root;
            string head = string.Format("Root Feature: {0}"
                , this.FeatureInstance.FeatureName);
            this.Header = MainWindow.GenerateTreeItemHeader(head, _ICON_SOURCE
                , Backgroud);
        }

        /// <summary>
        /// Tree Item Construct
        /// </summary>
        /// <param name="treeObj"></param>
        /// <returns></returns>
        public static TreeViewItemControl ConstructTreeItem(TreeObject treeObj)
        {
            FeatureTreeRoot projFile = treeObj as FeatureTreeRoot;
            if (projFile == null)
            {
                return null;
            }
            return new TreeItem_Root(projFile);
        }
        /// <summary>
        /// Context Generator
        /// </summary>
        /// <param name="treeRoot"></param>
        /// <returns></returns>
        public static ContextMenu GenerateContext(TreeViewItemControl treeItem, TreeRoot treeRoot)
        {
            FeatureTreeRoot root = treeRoot as FeatureTreeRoot;
            TreeItem_Root treeItem_Root = treeItem as TreeItem_Root;
            if (root == null
                || treeItem_Root == null)
            {
                return null;
            }

            // new group relationship
            MenuItem_Root newGroup = new MenuItem_Root(treeItem_Root, root)
            {
                Name = root.ID.ToString(),
                Header = "New Group Relationship"
            };
            newGroup.Click += new RoutedEventHandler(NewGroupRelationship_EventHandler);

            // new property
            MenuItem_Root newProperty = new MenuItem_Root(treeItem_Root, root)
            {
                Name = root.ID.ToString(),
                Header = "New Property"
            };
            newProperty.Click += new RoutedEventHandler(NewProperty_EventHandler);

            // new constaint
            MenuItem_Root newCons = new MenuItem_Root(treeItem_Root, root)
            {
                Name = root.ID.ToString(),
                Header = "New Cross Tree Constraint"
            };
            newCons.Click += new RoutedEventHandler(NewConstraints_EventHandler);

            // rename
            MenuItem_Root rename = new MenuItem_Root(treeItem_Root, root)
            {
                Name = root.ID.ToString(),
                Header = "Rename"
            };
            rename.Click += new RoutedEventHandler(Rename_EventHandler);

            // save
            MenuItem_Root save = new MenuItem_Root(treeItem_Root, root)
            {
                Name = root.ID.ToString(),
                Header = "Save"
            };
            save.Click += new RoutedEventHandler(Save_EventHandler);

            // Expand All
            MenuItem_Root expandAll = new MenuItem_Root(treeItem_Root, root)
            {
                Name = root.ID.ToString(),
                Header = "Expand All"
            };
            expandAll.Click += new RoutedEventHandler(ExpandAll_EventHandler);
            
            // All Products
            MenuItem_Root allProducts = new MenuItem_Root(treeItem_Root, root)
            {
                Name = root.ID.ToString(),
                Header = "All Products"
            };
            allProducts.Click += new RoutedEventHandler(AllProduct_EventHandler);
            

            ContextMenu contextMenu = new ContextMenu();
            contextMenu.Items.Add(newGroup);
            contextMenu.Items.Add(newProperty);
            contextMenu.Items.Add(new Separator());
            contextMenu.Items.Add(rename);
            return contextMenu;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void Delete_EventHanlder(object sender, RoutedEventArgs e)
        {
            try
            {
                if (sender is MenuItem_Constraint)
                {
                    MenuItem_Constraint cons = (MenuItem_Constraint)sender;
                    FeatureModelTree tree = (FeatureModelTree)cons.FeatureInstance.Tree;
                    tree.DeleteCrossTreeConstraint(cons.FeatureInstance);
                }
                else if (sender is MenuItem_GroupRelationship)
                {
                    MenuItem_GroupRelationship gR = (MenuItem_GroupRelationship)sender;
                    FeatureModelTree tree = (FeatureModelTree)gR.FeatureInstance.TreeOwner;
                    tree.DeleteExistInstance(gR.FeatureInstance);
                }
                //else if (sender is MenuItem_Relationship)
                //{
                //    MenuItem_Relationship r = (MenuItem_Relationship)sender;
                //    FeatureModelTree tree = (FeatureModelTree)r.FeatureInstance.TreeOwner;
                //    tree.DeleteExistInstance(r.FeatureInstance);
                //}
                else if (sender is MenuItem_Node)
                {
                    MenuItem_Node node = (MenuItem_Node)sender;
                    FeatureModelTree tree = (FeatureModelTree)node.FeatureInstance.TreeOwner;
                    tree.DeleteExistInstance(node.FeatureInstance);
                }
                else if (sender is MenuItem_NodeProperty)
                {
                    MenuItem_NodeProperty p = (MenuItem_NodeProperty)sender;
                    FeatureModelTree tree = (FeatureModelTree)p.FeatureInstance.TreeOwner;
                    tree.DeleteExistInstance(p.FeatureInstance);
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
        /// <summary>
        /// New Feature Tree Group Relationship
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void NewGroupRelationship_EventHandler(object sender, RoutedEventArgs e)
        {
            NewGroupRelationshipDialog dia = new NewGroupRelationshipDialog();
            dia.ShowDialog();

            if (dia.Dirty == false)
            {
                return;
            }

            if (sender is MenuItem_Node)
            {
                MenuItem_Node node = (MenuItem_Node)sender;

                FeatureModelTree tree = (FeatureModelTree)node.FeatureInstance.TreeOwner;
                tree.CreateGroupRelationship(node.FeatureInstance, dia.GroupRelationshipType);
            }
            else if(sender is MenuItem_Root)
            {
                MenuItem_Root root = (MenuItem_Root)sender;
                FeatureModelTree tree = (FeatureModelTree)root.FeatureInstance.TreeOwner;
                tree.CreateGroupRelationship(root.FeatureInstance, dia.GroupRelationshipType);

            }


        }
        /// <summary>
        /// New Cross Tree Constraints
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void NewConstraints_EventHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_Root root = sender as MenuItem_Root;
            if (root == null)
            {
                return;
            }
            FeatureModelTree tree = (FeatureModelTree)root.FeatureInstance.TreeOwner;
            NewConstraintsDialog dia = new NewConstraintsDialog(tree.AllFeatureNodes);
            dia.ShowDialog();
            if (dia.Dirty == false)
            {
                return;
            }

            tree.CreateConstraint(dia.NodeA.ID, dia.NodeB.ID, dia.ConstraintType);
        }
        /// <summary>
        /// New Property
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void NewProperty_EventHandler(object sender, RoutedEventArgs e)
        {
            NewPropertyDialog dia = new NewPropertyDialog(string.Empty, string.Empty);
            dia.ShowDialog();

            if (dia.Dirty == false)
            {
                return;
            }

            if (sender is MenuItem_Node)
            {
                MenuItem_Node node = (MenuItem_Node)sender;
                FeatureModelTree tree = (FeatureModelTree)node.FeatureInstance.TreeOwner;

                FeatureNodeProperty property = tree.CreateProperty(node.FeatureInstance, dia.PropertyName);
                property.PropertyValue = dia.PropertyValue;
            }
            else if (sender is MenuItem_Root)
            {
                MenuItem_Root root = (MenuItem_Root)sender;
                FeatureModelTree tree = (FeatureModelTree)root.FeatureInstance.TreeOwner;

                FeatureNodeProperty property = tree.CreateProperty(root.FeatureInstance, dia.PropertyName);
                property.PropertyValue = dia.PropertyValue;
            }
        }
        /// <summary>
        /// Rename
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void Rename_EventHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_Root root = sender as MenuItem_Root;
            if (root == null)
            {
                return;
            }

            RenameDialog dialog = new RenameDialog(root.FeatureInstance.FeatureName);
            dialog.ShowDialog();
            if (dialog.Dirty == false)
            {
                return;
            }

            root.FeatureInstance.FeatureName = dialog.NewName;

        }
        /// <summary>
        /// Save 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void Save_EventHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_Root root = sender as MenuItem_Root;
            if (root == null)
            {
                return;
            }
            FeatureModelTree tree = (FeatureModelTree)root.FeatureInstance.TreeOwner;
            tree.Save();

        }
        /// <summary>
        /// Expand All Item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void ExpandAll_EventHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_Root root = sender as MenuItem_Root;
            if (root == null)
            {
                return;
            }
            subExpand(root.TreeItem);
        }
        /// <summary>
        /// 
        /// </summary>
        private static void subExpand(TreeViewItem item)
        {
            item.IsExpanded = true;
            foreach (TreeViewItem i in item.Items)
            {
                subExpand(i);
            }
        }
        /// <summary>
        /// Event Handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void AllProduct_EventHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_Root root = sender as MenuItem_Root;
            if (root == null)
            {
                return;
            }
            FeatureModelTree fmTree = (FeatureModelTree)root.FeatureInstance.TreeOwner;
            FMProductCollection allProducts = fmTree.AllProducts();

            AllProductsWindows dialog = new AllProductsWindows(allProducts);
            dialog.Show();
        }

    }
}
